<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理系统登录</title>
    <link rel="stylesheet" href="./css/login.css">
    <link rel="shortcut icon" href="./images/logo.ico" type="image/x-icon">
</head>
<body>
    <form action="" class="formEle">
        <div class="login-reg">
            <strong>登录</strong>
            <a href="./register.html">注册</a>
        </div>
        <div>
            <label for="account">账号：</label>
            <input type="text" id="account" placeholder="请输入账号">
        </div>
        <div>
            <label for="password">密码：</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div>
            <button type="submit">登录</button>
            <button type="reset">重置</button>
        </div>
    </form>
    <script src="./js/ajax.js"></script>
    <script src="./js/cookie.js"></script>
    <script>
        var loginObj = {
            formEle: document.getElementsByClassName('formEle')[0],
            init: function() {
                this.eventAdd();
            },
            eventAdd: function() {
                var that = this;
                var btnSubmit = this.formEle.getElementsByTagName('button')[0];
                // console.log(btnSubmit);
                setCookie('account', "", -1);
                btnSubmit.onclick = function(e) {
                    var evt = e || window.event;
                    evt.preventDefault();
                    var account = that.formEle.account.value,
                        pawd = that.formEle.password.value;
                    // console.log(account,pawd);
                    if(account && pawd) {
                        var data = 'appkey=xiaobaibai_1590807635908&account=' + account + '&password=' + pawd;
                        ajax('POST', 'http://open.duyiedu.com/api/student/stuLogin', data, function(res){
                            // console.log(res);
                            if(res.status === 'success') {
                                setCookie('account', account);
                                window.location.href = './index.html';
                            }else{
                                alert(res.msg);
                            }
                        }, true)
                    }else{
                        alert('表单填写不完整！！');
                    }
                }   
            }

        }

        loginObj.init();


    </script>
</body>
</html>